package com.busionline.wotu.woochenandroid.http;

import com.busionline.wotu.woochenandroid.BuildConfig;
import com.busionline.wotu.woochenandroid.api.ApiService;
import com.busionline.wotu.woochenandroid.api.UrlConstainer;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *retrofit请求工具类
 *@author woochen123
 *@time 2018/5/15 15:40
 *@desc
 */

public class RetrofitUtil {
    public static ApiService api;

    private RetrofitUtil() {
    }

    static RetrofitUtil mInstance;

    public static RetrofitUtil getInstance() {
        if (mInstance == null) {
            synchronized (RetrofitUtil.class) {
                if (mInstance == null) {
                    mInstance = new RetrofitUtil();
                }
            }
        }
        return mInstance;
    }

    /**
     * 网络引擎初始化
     */
    public void init(){
        //声明日志类
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        //设定日志级别
        if(BuildConfig.DEBUG){
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        }else {
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }
        OkHttpClient okHttp = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttp)
                .baseUrl(UrlConstainer.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        api = retrofit.create(ApiService.class);
    }

    public static ApiService getApi(){
        if(api == null){
            throw new IllegalArgumentException("please init Retrofit first!");
        }
        return api;
    }

}
